package ko;

import java.util.Vector;

import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class KInput {

	public static String keyString = "";
	public static int lastKey;
	public static Boolean click = false;
	public static Boolean mouseDown = false;
	public static Boolean mouseUp = false;
	public static Boolean mousePressed = false;
	public static Boolean mouseReleased = false;
	public static Boolean mouseWheel = false;

	private static Boolean _enabled = false;
	private static Vector<Boolean> _key = new Vector<Boolean>(256);
	private static PointF _lastDown = new PointF();
	private static PointF mouse = new PointF();

	public static void enable(SurfaceView stage) {
		if (!_enabled && stage != null) {
			stage.setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View v, MotionEvent e) {
					mouse.x = e.getX() / K.SysRW;
					mouse.y = e.getY() / K.SysRH;
					switch (e.getAction()) {
					case MotionEvent.ACTION_DOWN:
						return KInput.onMotionDown(v, e);
					case MotionEvent.ACTION_UP:
						return KInput.onMotionUp(v, e);
					case MotionEvent.ACTION_MOVE:
						return KInput.onMotionMove(v, e);
					//case MotionEvent
					}
					return true;
				}
			});
			
			// stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			// stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			// stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			// stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			// stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			_enabled = true;
			Log.i("KInput", "enable");
		}
		return;
	}

	public static float mouseX() {
		return mouse.x;
	}

	public static float mouseY() {
		return mouse.y;
	}

	public static boolean onMotionDown(View v, MotionEvent e) {
		if (!mouseDown) {
			mouseDown = true;
			mouseUp = false;
			mousePressed = true;
			//Log.i("KInput", "onMouseDown");
		}
		return true;
	}

	public static boolean onMotionMove(View v, MotionEvent e) {
		// Log.i("KInput", "x:" + String.valueOf(motionX()) + " " + "y:" +
		// String.valueOf(motionY()));
		return true;
	}

	public static boolean onMotionUp(View v, MotionEvent e) {
		// click = _lastDown.equals(e.getX(), e.getY());
		mouseDown = false;
		mouseUp = true;
		mouseReleased = true;
		// Log.i("KInput", "onMouseUp");
		return true;
	}

	//
	public static void update() {
		if (mousePressed){
			mousePressed = false;
		}
		if (mouseReleased){
			mouseReleased = false;
		}
		if (mouseUp) {
			mouseUp = false;
		}
		if (click){
			click = false;
		}
		return;
	}

	public static Boolean check(int param1) {
		return _key.get(param1);
	}
	//
	// static private function onKeyUp(e:KeyboardEvent):void {
	// var keyCode:int = e.keyCode;
	//
	// if (_key[keyCode]){
	// _key[keyCode] = false;
	// }
	// }
	//
	// public static function get mouseX():int {
	// return K.stage.mouseX;
	// }
	//
	// public static function get mouseY():int {
	// return K.stage.mouseY;
	// }
	//
	// static public function ClickEntity(entity:KEntity, isClick:Boolean =
	// false):Boolean {
	// if (click && entity.containsMouse()) {
	// click = isClick;
	// return true;
	// }
	// return false;
	// }
	//
	// static private function onKeyDown(e:KeyboardEvent):void {
	// var keyCode:int = e.keyCode;
	// lastKey = keyCode;
	// if (!_key[keyCode]){
	// _key[keyCode] = true;
	// }
	// }
	//

	//

	//
	// //static private function onMouseWheel(e:MouseEvent):void {
	// //mouseWheel = true;
	// //_mouseWheelDelta = e.delta;
	// //}
}
